package com.woniu.darryringmall.ucenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.darryringmall.front.entity.Collect;
import com.woniu.darryringmall.front.entity.CollectVo;
import com.woniu.darryringmall.front.entity.Goods;
import com.woniu.darryringmall.front.entity.User;
import com.woniu.darryringmall.front.service.CollectService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("ucenter/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 进入我的收藏
     * @return
     */
    @PostMapping("list")
    public ResponseData list(HttpSession session){
        List<CollectVo> collectGoods=collectService.getByUserId(((User)session.getAttribute("user")).getId());
        return new ResponseData(collectGoods);
    }

    /**
     * 删除收藏
     */
    @ResponseBody
    @PostMapping("delete")
    public ResponseData delete( int id){
        collectService.delete(id);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 增加收藏
     */
    @ResponseBody
    @PostMapping("add")
    public ResponseData add(int goodsId,int goodsSpecId,HttpSession session){
        User loginUser= (User)session.getAttribute("user");
        Collect collect=new Collect();
        collect.setUserId(loginUser.getId());
        collect.setGoodsId(goodsId);
        collect.setGoodsSpecId(goodsSpecId);
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        collect.setRemarkTime(df.format(day));
        collectService.add(collect);
        return ResponseUtils.SUCCESS;
    }

}
